"""
团队模型
"""
from sqlalchemy import Column, String, Integer, Text, Enum, ForeignKey, DateTime, func
from sqlalchemy.orm import relationship
from .base import Base


class Team(Base):
    """团队表"""
    __tablename__ = "teams"
    
    name = Column(String(50), nullable=False, comment="团队名称")
    department_id = Column(Integer, ForeignKey("departments.id"), nullable=False, comment="所属部门ID")
    leader_id = Column(Integer, ForeignKey("employees.id"), comment="团队负责人ID")
    description = Column(Text, comment="团队描述")
    status = Column(Enum("active", "inactive", name="team_status"), 
                   default="active", comment="状态")
    
    # 关系
    department = relationship("Department", back_populates="teams")
    leader = relationship("Employee", back_populates="led_teams")
    employee_assignments = relationship("EmployeeTeam", back_populates="team")
    traffic_statistics = relationship("TrafficStatistics", back_populates="team")
    
    def __repr__(self):
        return f"<Team(id={self.id}, name='{self.name}', department_id={self.department_id})>"


class EmployeeTeam(Base):
    """员工团队关系表"""
    __tablename__ = "employee_teams"
    
    employee_id = Column(Integer, ForeignKey("employees.id"), nullable=False, comment="员工ID")
    team_id = Column(Integer, ForeignKey("teams.id"), nullable=False, comment="团队ID")
    role = Column(Enum("member", "leader", "deputy", name="team_role"), 
                 default="member", comment="角色")
    join_date = Column(DateTime, nullable=False, comment="加入日期")
    leave_date = Column(DateTime, comment="离开日期")
    status = Column(Enum("active", "inactive", name="assignment_status"), 
                   default="active", comment="状态")
    
    # 关系
    employee = relationship("Employee", back_populates="team_assignments")
    team = relationship("Team", back_populates="employee_assignments")
    
    def __repr__(self):
        return f"<EmployeeTeam(employee_id={self.employee_id}, team_id={self.team_id}, role='{self.role}')>"
